{% extends 'base.html' %}


{% block pub_active %}
    active
{% endblock %}

{% block content %}

    <h3 class="sub-header">出版社列表</h3>
    <div class="table-responsive">

        <a class="btn btn-primary btn-sm" href="/publisher_add/"> 添加 </a>

        <table class="table table-striped">
            <thead>
            <tr>
                <th>序号</th>
                <th>ID</th>
                <th>出版社名称</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>

            {% for publisher in all_publishers %}

                <tr>
                    <td>{{ forloop.counter }}</td>
                    <td>{{ publisher.pk }}</td>
                    <td>{{ publisher }}</td>
                    <td>
                        {#                                <a class="btn btn-danger btn-sm" href="{% url 'delete' 'publisher' publisher.pk %}">删除</a>#}
                        <button del_id="{{ publisher.pk }}" url="{% url 'delete' 'publisher' publisher.pk %}"
                                class="btn btn-danger btn-sm ">删除
                        </button>
                        <a class="btn btn-warning btn-sm" href="/publisher_edit/?pk={{ publisher.pk }}">编辑</a>
                    </td>
                </tr>

            {% endfor %}

            </tbody>
        </table>
    </div>

{% endblock %}



{% block js %}
    <script src="/s1/js/sweetalert.min.js"></script>


    <script>

        $('.btn-danger').click(function () {

            swal({
                title: "Are you sure?",
                text: "Once deleted, you will not be able to recover this imaginary file!",
                icon: "warning",
                buttons: true,
                dangerMode: true,
            })
                .then((willDelete) => {
                    if (willDelete) {

                        $.ajax({
                            {#url: '/publisher_del/' + $(this).attr('del_id') + '/',#}
                            url: $(this).attr('url'),
                            success: (res) => {
                                // 后端删除成功后，在前端删除数据
                                if (res.status) {
                                    $(this).parent().parent().remove()
                                    swal("Poof! Your imaginary file has been deleted!", {
                                        icon: "success",
                                    });
                                }
                            }
                        })

                    } else {
                        swal("Your imaginary file is safe!");
                    }
                });


        })

        {#function f() {#}
        {##}
        {# } #}
        {#() => {}  //  不会改变this的指向#}

    </script>

{% endblock %}